<?php

class order {
/**
*   @desc Variable Declaration with default value
*/
	private $_id;   // KEY ATTR. WITH AUTOINCREMENT
     private $_productid;  
     private $_userid;  
     private $_productqty;  
     private $_productprice;  
     private $_productsubtotal;  
     private $_createdate;  
     private $_status;  
     
/**
*   @desc   CONSTRUCTOR METHOD
*/
	function __construct() {
		global $DBOBJ;
		$this->_obj = $DBOBJ;
		$this->_id = null; 
		$this->_productid = null; 
		$this->_userid = null; 
		$this->_productqty = null; 
		$this->_productprice = null; 
		$this->_productsubtotal = null; 
		$this->_createdate = null; 
		$this->_status = null; 
	}

/**
*   @desc   DECONSTRUCTOR METHOD
*/
	function __destruct() {
		unset($this->_obj);
	}

/**
*   @desc   GETTER METHODS
*/

     public function getid() {
     	return $this->_id;
     }
     
     public function getproductid() {
     	return $this->_productid;
     }
     
     public function getuserid() {
     	return $this->_userid;
     }
     
     public function getproductqty() {
     	return $this->_productqty;
     }
     
     public function getproductprice() {
     	return $this->_productprice;
     }
     
     public function getproductsubtotal() {
     	return $this->_productsubtotal;
     }
     
     public function getcreatedate() {
     	return $this->_createdate;
     }
     
     public function getstatus() {
     	return $this->_status;
     }
     
/**
*   @desc   SETTER METHODS
*/

     public function setid($id) {
     	 $this->_id =  $id;
     }
     
     public function setproductid($productid) {
     	 $this->_productid =  $productid;
     }
     
     public function setuserid($userid) {
     	 $this->_userid =  $userid;
     }
     
     public function setproductqty($productqty) {
     	 $this->_productqty =  $productqty;
     }
     
     public function setproductprice($productprice) {
     	 $this->_productprice =  $productprice;
     }
     
     public function setproductsubtotal($productsubtotal) {
     	 $this->_productsubtotal =  $productsubtotal;
     }
     
     public function setcreatedate($createdate) {
     	 $this->_createdate =  $createdate;
     }
     
     public function setstatus($status) {
     	 $this->_status =  $status;
     }
     
/**
*   @desc   SELECT METHOD / LOAD
*/

	public function select($id) {
		 $sql =  "SELECT * FROM `tbl_order` WHERE `id` = '". $id ."' ";
		 $row =  $this->_obj->select($sql); 

		 if (count($row) > 0) {
			 $this->_id = $row[0]['id'];
     		 $this->_productid = $row[0]['productid'];
     		 $this->_userid = $row[0]['userid'];
     		 $this->_productqty = $row[0]['productqty'];
     		 $this->_productprice = $row[0]['productprice'];
     		 $this->_productsubtotal = $row[0]['productsubtotal'];
     		 $this->_createdate = $row[0]['createdate'];
     		 $this->_status = $row[0]['status'];
     			 return $row[0];
		}else {
			 return;
		}
	}
/**
*   @desc   set All
*/
	 public function setAll($dataArray = array()) {
		 if (count($dataArray) <= 0) {
			return;
		}

		foreach ($dataArray as $key => $value) {
			$classKey = '_' . $key;
			$this->$classKey = $value;
		}
	}

/**
*   @desc   get All
*/

	 public function getAll($FORMAT = 'ARRAY') {
		$dataArray = new stdClass();
		$dataArray->id = $this->getid();
     	$dataArray->productid = $this->getproductid();
     	$dataArray->userid = $this->getuserid();
     	$dataArray->productqty = $this->getproductqty();
     	$dataArray->productprice = $this->getproductprice();
     	$dataArray->productsubtotal = $this->getproductsubtotal();
     	$dataArray->createdate = $this->getcreatedate();
     	$dataArray->status = $this->getstatus();
     	if ($FORMAT == 'ARRAY') {
			return (array) $dataArray;
		}
		return $dataArray;
	}
/**
*   @desc   DELETE
*/
	public function delete($id) {
		 $sql = "DELETE FROM `tbl_order` WHERE `id` = '". $id ."'";
		 $result = $this->_obj->sql_query($sql);
	}

/**
*   @desc   INSERT
*/
	public function insert() {
		 $this->id = ""; // clear key for autoincrement
		 $sql = "INSERT INTO `tbl_order` ( `id`,`productid`,`userid`,`productqty`,`productprice`,`productsubtotal`,`createdate`,`status` ) VALUES ( NULL,'".$this->_productid."','".$this->_userid."','".$this->_productqty."','".$this->_productprice."','".$this->_productsubtotal."','".$this->_createdate."','".$this->_status."' )";
		 $result = $this->_obj->insert($sql);
		 return $result;
	}

/**
*   @desc   UPDATE
*/
	public function update($id) {
		 $sql = " UPDATE `tbl_order` SET  `id` = '".$this->_id."' , `productid` = '".$this->_productid."' , `userid` = '".$this->_userid."' , `productqty` = '".$this->_productqty."' , `productprice` = '".$this->_productprice."' , `productsubtotal` = '".$this->_productsubtotal."' , `createdate` = '".$this->_createdate."' , `status` = '".$this->_status."'  WHERE `id` =   '".$id."' ";
		 $result = $this->_obj->sql_query($sql);
	}
	
	public function getAllorderList($params = "*", $where = "", $orderby = "id", $limit = "") {
		$orderby = "ORDER BY " . $orderby;
		$sql = "SELECT " . $params . " FROM `tbl_order` a  
			join  user u on u.userid = a.userid 
			join  product p on a.productid = p.id 	 
		" . $where . " " . $orderby . " " . $limit;
		#echo $sql; exit;
		$row = $this->_obj->select($sql);
		if ($row) {
			return $row;
		}
		return array();
	}
	
	
	/**
*   @desc   COUNT
*/
	public function getCountorder($where = '') {
		 $sql = "SELECT count(*) as cnt  FROM `tbl_order` a
		 join  user u on u.userid = a.userid 
		 join  product p on a.productid = p.id ". $where;
		 $result = $this->_obj->select($sql);
		 return $result['0']['cnt'];
	}


	
	
	
	
	public function performListAction($action = "", $ids = "''") {
		switch ($action) {
			case "delete":
				$sql = "DELETE FROM `tbl_order` WHERE `id` IN (" . $ids . ")";
			break;
			default:
				$sql = "";
		}
		if ($sql == "") {
			return false;
		}
		$result = $this->_obj->sql_query($sql);
		return true;
    }

} 
?>